<!DOCTYPE html>
<html>
<body>
<link rel="stylesheet" href="/javascripts/zTree_v3/css/demo.css" type="text/css">
<link rel="stylesheet" href="/javascripts/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
<input type="button" onclick="toAdd();" value="添加">
<input type="hidden" id="parent">
<ul id="myTree" class="ztree">
    <div align="center"><img src="/images/loading.gif" width="20px">&nbsp;&nbsp;载入中</div>
</ul>
<script src="/javascripts/jquery-3.3.1.js"></script>
<script src="/javascripts/zTree_v3/js/jquery.ztree.all.js"></script>
<script>
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            addDiyDom: addDiyDom
        }
    };

    function addDiyDom(treeId, treeNode) {
        if (treeNode.id==null)return;
        var aObj = $("#" + treeNode.tId+"_a");
        var editStr = "<span class='button edit' title='修改' id='editBtn_"+treeNode.id+ "' onfocus='this.blur();' treenode_edit='' style=''></span>"
        editStr+="<span class='button remove' title='删除' id='deleteBtn_"+treeNode.id+ "' onfocus='this.blur();' treenode_remove='' style=''></span>"
        aObj.append(editStr);
        var editBtn = $("#editBtn_"+treeNode.id);
        if (editBtn) editBtn.bind("click", function(){window.location.href="/menu/update?id="+treeNode.id});
        var deleteBtn = $("#deleteBtn_"+treeNode.id);
        if (deleteBtn) deleteBtn.bind("click", function(){window.location.href="/menu/del?id="+treeNode.id});
    };

    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
    $(document).ready(function () {
        $.ajax({
            url: '/menu/getTreeNode',
            type:"post",
            dataType: 'JSON',
            success: function(data){
                var rootNode = data.rootChildren;
                var treeNode = data.treeChildren;
                var rootChildren = new Array();
                for(var i=0;i<rootNode.length;i++){
                    var treeChildren = new Array();
                    for(var j=0;j<treeNode.length;j++){
                        if(treeNode[j].parent==rootNode[i]._id){
                            treeChildren.push({name:treeNode[j].name,id:treeNode[j]._id,click:"getParent('');"});
                        }
                    }
                    rootChildren.push({name:rootNode[i].name,id:rootNode[i]._id,click:"getParent('"+rootNode[i]._id+"');",children:treeChildren});
                }
                var sys = {name:"系统", click:"getParent('0');",open:true, children:rootChildren};
                zTreeObj = $.fn.zTree.init($("#myTree"), setting, [sys]);
            },
            error: function(){
                alert("查询节点失败");
            }
        });
    });
    function getParent(id) {
        $("#parent").val(id);
    }
    function toAdd() {
        var parent = $("#parent").val();
        if(parent==""){
            alert("请选择菜单节点进行添加");
            return;
        }
        window.location.href="/menu/add?id="+parent;
    }
</script>
</body>
</html>